/*
* iCutCopies: 将目标切割成多少份
* iMaxWidth: 最小屏幕宽度(单位:px), 如果可视窗口宽度 < iMaxWidth, 则
* */
export const initREM = (iCutCopies = 25, iMaxWidth = 720) => {
	//获取Html元素
	const oHtml          = document.getElementsByTagName('HTML')[0];
	//获取当前可视窗口宽度
	const iWindowWidth   = oHtml.getBoundingClientRect().right;
	//设置Html元素的字体大小
	oHtml.style.fontSize = (iWindowWidth < iMaxWidth ? iMaxWidth : iWindowWidth) / iCutCopies + 'px'
}

//判断对象是否为DOM对象
export const isDOM = (obj) => {
	return Boolean(
		(typeof HTMLElement === 'object') ?
		obj instanceof HTMLElement :
		obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string'
	)
}

//元素高度等于可视窗口高度
export const eleEqualClientHeight = (ele) => {
	try {
		document.querySelector(ele).style.height = document.documentElement.clientHeight + 'px'
	} catch (e) {
		console.log('param is not an element node.', e);
	}
}

//将router-view中的窗口高度改成100%
export const viewHeightPercent100 = (ele) => {
	//.el-scrollbar__view高度100%
	ele.parentNode.style.height   = '100%'
	ele.parentNode.style.overflow = 'hidden'
	
	//去滑块样式
	ele.parentNode.parentNode.nextSibling.style.display             = 'none'
	ele.parentNode.parentNode.nextSibling.nextSibling.style.display = 'none'
}

//时间戳格式化函数
const dbNum             = (num) => {
	return num >= 10 ? num : '0' + num
}
export const dateFormat = (format, timestamp) => {
	const oDate = new Date(timestamp)
	const year  = oDate.getFullYear()
	const month = oDate.getMonth() + 1
	const date  = oDate.getDate()
	const hour  = oDate.getHours()
	const min   = oDate.getMinutes()
	const sec   = oDate.getSeconds()
	
	if (format === 'Y-m-d') return `${dbNum(year)}-${dbNum(month)}-${dbNum(date)}`
	if (format === 'Y-m-d H:m:s') return `${dbNum(year)}-${dbNum(month)}-${dbNum(date)} ${dbNum(hour)}:${dbNum(min)}:${dbNum(sec)}`
	return timestamp
}


/*my first demo some code*****************************************************************/
export const getByClass = (aParent, sClass) => {
	var aResult = [];
	var aEle    = aParent.getElementsByTagName('*');
	for (var i = 0; i < aEle.length; i++) {
		if (aEle[i].className === sClass) {
			aResult.push(aEle[i]);
		}
	}
	return aResult;
}

export const getStyle = (obj, name, value) => {
	if (obj.currentStyle) {
		if (arguments.length === 2) {
			return obj.currentStyle[name];  //IE
		}
		else if (arguments.length === 3) {
			//alert('IE');
			obj.style[name] = value;  //上面那招不能用于修改
		}
	}
	else {
		if (arguments.length === 2) {
			return getComputedStyle(obj, 1)[name];  //FF Chrome
		}
		else if (arguments.length === 3) {
			//alert('FF');
			obj.style[name] = value;  //上面那招不能用于修改
		}
	}
}

export const startMove = (obj, json, fnEnd) => {
	clearInterval(obj.timer);
	obj.timer = setInterval(function () {
		var bStop = true;
		for (var attr in json) {
			if (attr === 'opacity') {
				var cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
			}
			else {
				var cur = parseInt(getStyle(obj, attr));
			}
			var speed = (json[attr] - cur) / 6;
			speed     = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
			
			if (attr === 'opacity') {
				obj.style.filter = 'alpha(opacity:' + (cur + speed) + ')';
				obj.style[attr]  = (cur + speed) / 100;
			}
			else {
				obj.style[attr] = cur + speed + 'px';
			}
			
			if (cur !== json[attr]) bStop = false;
		}
		if (bStop) {
			clearInterval(obj.timer);
			if (fnEnd) fnEnd();
		}
	}, 30);
};
/*my first demo some code*****************************************************************/

//HeadChange
function HeadChange(id) {
	var _this         = this;
	this.oChange      = document.getElementById(id);
	this.scrollTop    = document.documentElement.scrollTop || document.body.scrollTop;
	this.allNodes     = this.oChange.getElementsByTagName('*');
	this.oLogo        = this.oChange.getElementsByTagName('div')[0].children[0];
	this.oMenu        = this.oChange.getElementsByTagName('div')[0].children[1];
	this.oIphone      = this.oChange.getElementsByTagName('div')[0].children[2];
	this.iClientWidth = document.documentElement.clientWidth;
	if (this.scrollTop === 0) {
		for (var i = 0; i < this.allNodes.length; i++) {
			this.allNodes[i].style.transform = '';
		}
	}
	else {
		for (var i = 0; i < this.allNodes.length; i++) {
			this.allNodes[i].style.transform = 'scale(0.95,0.95)';
		}
	}
}

//expansionMenu
function Expansion(id) {
	var _this         = this;
	this.oEpsnUl      = document.getElementById(id);
	this.oBtnH6       = this.oEpsnUl.getElementsByTagName('h6')[0];
	this.aList        = this.oEpsnUl.getElementsByTagName('li');
	this.iClientWidth = document.documentElement.clientWidth;
	if (this.iClientWidth > 983) {
		for (var i = 0; i < this.aList.length; i++) {
			this.aList[i].style.display = 'block';
			this.aList[i].style.float   = 'left';
		}
	}
	else {
		for (var i = 0; i < this.aList.length; i++) {
			this.aList[i].style.display = 'none';
			this.aList[i].style.float   = 'none';
		}
		this.oBtnH6.onclick = function () {
			_this.fnEpsn();
		}
	}
	
}

Expansion.prototype.fnEpsn = function () {
	if (getStyle(this.aList[0], 'display') === 'none') {
		for (var i = 0; i < this.aList.length; i++) {
			this.aList[i].style.display = 'block';
			this.aList[i].style.float   = 'none';
		}
	}
	else {
		for (var i = 0; i < this.aList.length; i++) {
			this.aList[i].style.display = 'none';
			this.aList[i].style.float   = 'none';
		}
	}
}

//首屏onload
function TheFirstScreen(id) {
	var _this = this;
	
	//核心是取出可视窗口宽度,其它相应的按其比例自动调整
	this.iClientWidth = document.documentElement.clientWidth;
	this.oDivDemoBox  = document.getElementById(id);
	this.oUlDemo      = this.oDivDemoBox.getElementsByTagName('ul')[0];
	this.aImgDemo     = this.oUlDemo.getElementsByTagName('img');
	
	//窗口变化时,改变大图片的width, 随后它的height会按比例自适应
	for (var i = 0; i < this.aImgDemo.length; i++) {
		this.aImgDemo[i].style.width = this.iClientWidth + 'px';
	}
	
	//窗口变化时,改变首屏盒子的height
	this.oDivDemoBox.style.height = Math.round(this.iClientWidth * (877 / 1903)) + 'px';
	
	//窗口变化时,改变ul的长度
	this.oUlDemo.style.width = (this.iClientWidth * this.aImgDemo.length) + 'px';
	
	//窗口变化时,改变左右按钮的top left right width height
	this.oDivPrev  = getByClass(this.oDivDemoBox, 'prev')[0];
	this.oDivNext  = getByClass(this.oDivDemoBox, 'next')[0];
	this.oDivRound = getByClass(this.oDivDemoBox, 'round')[0];
	this.aImgRound = this.oDivRound.getElementsByTagName('img');
	
	this.oDivPrev.style.top                = Math.round((parseFloat(this.oDivDemoBox.style.height) - this.oDivPrev.offsetHeight) / 2) + 'px';
	this.oDivPrev.style.left               = Math.round(this.iClientWidth * (100 / 1903)) + 'px';
	this.oDivPrev.children[0].style.width  = Math.round(this.iClientWidth * (42 / 1903)) + 'px';
	this.oDivPrev.children[0].style.height = Math.round(this.iClientWidth * (105 / 1903)) + 'px';
	
	this.oDivNext.style.top                = Math.round((parseFloat(this.oDivDemoBox.style.height) - this.oDivNext.offsetHeight) / 2) + 'px';
	this.oDivNext.style.right              = Math.round(this.iClientWidth * (100 / 1903)) + 'px';
	this.oDivNext.children[0].style.width  = Math.round(this.iClientWidth * (42 / 1903)) + 'px';
	this.oDivNext.children[0].style.height = Math.round(this.iClientWidth * (105 / 1903)) + 'px';
	
	//窗口变化时,改变圆导航的width height
	this.oDivRound.style.bottom = Math.round(this.iClientWidth * (45 / 1903)) + 'px';
	for (var i = 0; i < this.aImgRound.length; i++) {
		//圆航行不可以小于4px,因为用户点不到
		if (Math.round(this.iClientWidth * (8 / 1903)) < 4) {
			this.aImgRound[i].style.width  = 4 + 'px';
			this.aImgRound[i].style.height = 4 + 'px';
		}
		else {
			this.aImgRound[i].style.width  = Math.round(this.iClientWidth * (8 / 1903)) + 'px';
			this.aImgRound[i].style.height = Math.round(this.iClientWidth * (8 / 1903)) + 'px';
		}
	}
	/*prev 和 next*/
	
	//左右按钮淡入淡出部分
	this.oDivDemoBox.onmouseover = function () {
		_this.fnMseOver();
	}
	this.oDivDemoBox.onmouseout  = function () {
		_this.fnMseOut();
	}
	
	//左右按钮移入移出效果
	this.iDiscPrevX = this.oDivPrev.children[0].offsetWidth;
	this.iDiscPrevY = this.oDivPrev.children[0].offsetHeight;
	this.iDiscNextX = this.oDivNext.children[0].offsetWidth;
	this.iDiscNextY = this.oDivNext.children[0].offsetHeight;
	
	myAddEvent(this.oDivPrev, 'mouseover', function () {
		startMove(this.children[0], {width: _this.iDiscPrevX + 6, height: _this.iDiscPrevY + 6});
	});
	myAddEvent(this.oDivPrev, 'mouseout', function () {
		startMove(this.children[0], {width: _this.iDiscPrevX, height: _this.iDiscPrevY});
	});
	
	myAddEvent(this.oDivNext, 'mouseover', function () {
		startMove(this.children[0], {width: _this.iDiscNextX + 6, height: _this.iDiscNextY + 6});
	});
	myAddEvent(this.oDivNext, 'mouseout', function () {
		startMove(this.children[0], {width: _this.iDiscNextX, height: _this.iDiscNextY});
	});
	
	// 小圆导航, oUlDemo运动部分
	this.now      = 0;
	this.iLiWidth = this.aImgDemo[0].offsetWidth;
	//document.title = this.now;
	
	for (var i = 0; i < this.aImgRound.length; i++) {
		this.aImgRound[i].index = i;
		
		//按小圆导航, 切换首屏图
		this.aImgRound[i].onclick = function () {
			_this.fnRoundOnClick(this);
		}
		
		//鼠标移入小圆导航, 改变样式
		myAddEvent(this.aImgRound[i], 'mouseover', function () {
			if (this.index === _this.now) {
				this.src = '../images/round2.png';
			}
			else {
				this.src = '../images/round3.png';
			}
		});
		myAddEvent(this.aImgRound[i], 'mouseout', function () {
			if (this.index === _this.now) {
				this.src = '../images/round2.png';
			}
			else {
				this.src = '../images/round1.png';
			}
		});
		
		//oUlDemo运动部分
		this.oDivPrev.onclick = function () {
			_this.fnPrevOnClk();
		}
		this.oDivNext.onclick = function () {
			_this.fnNextOnClk();
		}
	}
	
	//首屏的setInterval部分
	clearInterval(this.timer);
	this.timer                = setInterval(this.oDivNext.onclick, 3000);
	this.oDivPrev.onmouseover = this.oDivNext.onmouseover = this.oDivRound.onmouseover = this.oUlDemo.onmouseover = function () {
		_this.fnSetIntervalMseOver();
	}
	this.oDivPrev.onmouseout  = this.oDivNext.onmouseout = this.oDivRound.onmouseout = this.oUlDemo.onmouseout = function () {
		_this.fnSetIntervalMseOut();
	}
	
}

//左右按钮淡入淡出
TheFirstScreen.prototype.fnMseOver            = function () {
	startMove(this.oDivPrev, {opacity: 100});
	startMove(this.oDivNext, {opacity: 100});
}
TheFirstScreen.prototype.fnMseOut             = function () {
	startMove(this.oDivPrev, {opacity: 0});
	startMove(this.oDivNext, {opacity: 0});
}
//点击圆导航
TheFirstScreen.prototype.fnRoundOnClick       = function (oImg) {
	for (var i = 0; i < this.aImgRound.length; i++) {
		this.aImgRound[i].src = '../images/round1.png';
	}
	oImg.src = '../images/round2.png';
	this.now = oImg.index;
	//document.title = this.now;
	startMove(this.oUlDemo, {left: -this.now * this.iLiWidth});
}
//点击上1个按钮
TheFirstScreen.prototype.fnPrevOnClk          = function () {
	this.now--;
	if (this.now === -1) {
		this.now = this.aImgDemo.length - 1;
	}
	startMove(this.oUlDemo, {left: -this.now * this.iLiWidth});
	//按oDivPrev时, 小圆图标切换
	for (var i = 0; i < this.aImgRound.length; i++) {
		this.aImgRound[i].src        = '../images/round1.png';
		this.aImgRound[this.now].src = '../images/round2.png';
	}
	//document.title = this.now;
}
//点击下1个按钮
TheFirstScreen.prototype.fnNextOnClk          = function () {
	var _this = this;
	this.now++;
	if (this.now === this.aImgDemo.length) {
		this.now = 0;
		startMove(this.oUlDemo, {left: 0});
	}
	else {
		startMove(this.oUlDemo, {left: -this.now * this.iLiWidth}, function () {
			//当图片滚动的时候, 拖动窗口大小, 当前图片会一直卡在左上角位置而不会串行, 否则图片不会卡在左上角;
			_this.oUlDemo.style.left = (-_this.iLiWidth * _this.now) + 'px';
			//document.title = '已复位: '+_this.now;
		});
	}
	//按oDivPrev时, 小圆图标切换
	for (var i = 0; i < this.aImgRound.length; i++) {
		this.aImgRound[i].src        = '../images/round1.png';
		this.aImgRound[this.now].src = '../images/round2.png';
	}
	//document.title = this.now;
}
//计时器Next
TheFirstScreen.prototype.fnSetIntervalMseOver = function () {
	clearInterval(this.timer);
	//document.title = 'stop';
}
TheFirstScreen.prototype.fnSetIntervalMseOut  = function () {
	clearInterval(this.timer);
	this.timer = setInterval(this.oDivNext.onclick, 3000);
	//document.title = this.now;
}

//首屏onresize
function ReSizeTheFirstScreen(id) {
	clearInterval(oDeo.timer);
	var _this = this;
	
	//核心是取出可视窗口宽度,其它相应的按其比例自动调整
	this.iClientWidth = document.documentElement.clientWidth;
	this.oDivDemoBox  = document.getElementById(id);
	this.oUlDemo      = this.oDivDemoBox.getElementsByTagName('ul')[0];
	this.aImgDemo     = this.oUlDemo.getElementsByTagName('img');
	
	//窗口变化时,改变大图片的width, 随后它的height会按比例自适应
	for (var i = 0; i < this.aImgDemo.length; i++) {
		this.aImgDemo[i].style.width = this.iClientWidth + 'px';
	}
	
	//窗口变化时,改变首屏盒子的height
	this.oDivDemoBox.style.height = Math.round(this.iClientWidth * (877 / 1903)) + 'px';
	
	//窗口变化时,改变ul的长度
	this.oUlDemo.style.width = (this.iClientWidth * this.aImgDemo.length) + 'px';
	
	//当前图片会一直卡在左上角位置而不会串行
	oDeo.iLiWidth           = this.aImgDemo[0].offsetWidth;
	this.oUlDemo.style.left = -(oDeo.iLiWidth * oDeo.now) + 'px';
	//document.title = '我在这'+oDeo.now;
	
	//窗口变化时,改变左右按钮的top left right width height
	this.oDivPrev  = getByClass(this.oDivDemoBox, 'prev')[0];
	this.oDivNext  = getByClass(this.oDivDemoBox, 'next')[0];
	this.oDivRound = getByClass(this.oDivDemoBox, 'round')[0];
	this.aImgRound = this.oDivRound.getElementsByTagName('img');
	
	this.oDivPrev.style.top                = Math.round((parseFloat(this.oDivDemoBox.style.height) - this.oDivPrev.offsetHeight) / 2) + 'px';
	this.oDivPrev.style.left               = Math.round(this.iClientWidth * (100 / 1903)) + 'px';
	this.oDivPrev.children[0].style.width  = Math.round(this.iClientWidth * (42 / 1903)) + 'px';
	this.oDivPrev.children[0].style.height = Math.round(this.iClientWidth * (105 / 1903)) + 'px';
	
	this.oDivNext.style.top                = Math.round((parseFloat(this.oDivDemoBox.style.height) - this.oDivNext.offsetHeight) / 2) + 'px';
	this.oDivNext.style.right              = Math.round(this.iClientWidth * (100 / 1903)) + 'px';
	this.oDivNext.children[0].style.width  = Math.round(this.iClientWidth * (42 / 1903)) + 'px';
	this.oDivNext.children[0].style.height = Math.round(this.iClientWidth * (105 / 1903)) + 'px';
	
	//窗口变化时,改变圆导航的width height
	this.oDivRound.style.bottom = Math.round(this.iClientWidth * (45 / 1903)) + 'px';
	for (var i = 0; i < this.aImgRound.length; i++) {
		//圆航行不可以小于4px,因为用户点不到
		if (Math.round(this.iClientWidth * (8 / 1903)) < 4) {
			this.aImgRound[i].style.width  = 4 + 'px';
			this.aImgRound[i].style.height = 4 + 'px';
		}
		else {
			this.aImgRound[i].style.width  = Math.round(this.iClientWidth * (8 / 1903)) + 'px';
			this.aImgRound[i].style.height = Math.round(this.iClientWidth * (8 / 1903)) + 'px';
		}
	}
	
	//左右按钮移入移出效果
	this.iDiscPrevX = this.oDivPrev.children[0].offsetWidth;
	this.iDiscPrevY = this.oDivPrev.children[0].offsetHeight;
	this.iDiscNextX = this.oDivNext.children[0].offsetWidth;
	this.iDiscNextY = this.oDivNext.children[0].offsetHeight;
	
	myAddEvent(this.oDivPrev, 'mouseover', function () {
		startMove(this.children[0], {width: _this.iDiscPrevX + 6, height: _this.iDiscPrevY + 6});
	});
	myAddEvent(this.oDivPrev, 'mouseout', function () {
		startMove(this.children[0], {width: _this.iDiscPrevX, height: _this.iDiscPrevY});
	});
	
	myAddEvent(this.oDivNext, 'mouseover', function () {
		startMove(this.children[0], {width: _this.iDiscNextX + 6, height: _this.iDiscNextY + 6});
	});
	myAddEvent(this.oDivNext, 'mouseout', function () {
		startMove(this.children[0], {width: _this.iDiscNextX, height: _this.iDiscNextY});
	});
	
	//首屏的setInterval部分
	clearInterval(oDeo.timer);
	oDeo.timer = setInterval(oDeo.oDivNext.onclick, 3000);
	
}

//subset部分
function Subset(id) {
	this.oDivMenu = document.getElementById(id);
	this.aLiMenu  = this.oDivMenu.getElementsByTagName('li');
	
	for (var i = 0; i < this.aLiMenu.length; i++) {
		this.aLiMenu[i].style.height = Math.round(this.aLiMenu[0].offsetWidth / 3) + 'px';
	}
}

//nav部分
function Nav(id) {
	//nav部分
	var _this         = this;
	this.oDivNav      = document.getElementById(id);
	this.aSpanNav     = this.oDivNav.getElementsByTagName('span');
	this.aLiNav       = this.oDivNav.getElementsByTagName('li');
	this.iClientWidth = document.documentElement.clientWidth;
	
	if (this.iClientWidth > 983) {
		//document.title = this.iClientWidth;
		for (var i = 0; i < this.aLiNav.length; i++) {
			this.aLiNav[i].style.display = 'list-item';
		}
		for (var i = 0; i < this.aSpanNav.length; i++) {
			this.aSpanNav[i].onclick       = null;
			this.aSpanNav[i].style.display = 'none';
		}
	}
	else {
		for (var i = 0; i < this.aLiNav.length; i++) {
			this.aLiNav[i].style.display = 'none';
		}
		for (var i = 0; i < this.aSpanNav.length; i++) {
			this.aSpanNav[i].onclick       = function () {
				_this.fnAddition(this);
				return false;
			}
			this.aSpanNav[i].style.display = 'inline-block';
		}
	}
}

Nav.prototype.fnAddition = function (oSpan) {
	var aBtn = oSpan.parentNode.parentNode.children;
	
	for (var i = 0; i < aBtn.length; i++) {
		//alert(typeof(aBtn[i]));
		if (aBtn[i].nodeName === 'LI') {
			if (aBtn[i].style.display === 'list-item') {
				aBtn[i].style.display = 'none';
				oSpan.style.transform = '';
			}
			else {
				aBtn[i].style.display = 'list-item';
				oSpan.style.transform = 'rotate(45deg)';
			}
		}
		else {
			continue;
		}
	}
}

//对联
function Couplet(id) {
	var _this      = this;
	this.oDiv      = document.getElementById(id);
	this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop;//滚动条高度, 主流浏览器 || IE
	this.clientH   = document.documentElement.clientHeight || document.body.clientHeight;//可视窗口高度, 主流浏览器 || IE
	this.iTarget   = Math.round((this.clientH - this.oDiv.offsetHeight) / 1 + this.scrollTop);//可视窗口高度 - div高度 + 滚动条高度
	//this.oDiv.style.top = this.iTarget + 'px';
	startMove(this.oDiv, {top: this.iTarget});
	
	this.oClose = this.oDiv.getElementsByTagName('div')[1];
	
	this.oClose.onclick = function () {
		_this.fnClose();
	}
}

Couplet.prototype.fnClose = function () {
	var _this = this;
	//this.oDiv.style.display = 'none';
	startMove(this.oDiv, {opacity: 0}, function () {
		_this.oDiv.style.display = 'none';
	});
}

//gotoTop
function GotoTop(id) {
	var _this         = this;
	this.oDivGoTop    = document.getElementById(id);
	this.scrollTop    = document.documentElement.scrollTop || document.body.scrollTop;
	this.iClientWidth = document.documentElement.clientWidth;
	//document.title = this.scrollTop;
	if (this.iClientWidth > 983) {
		if (this.scrollTop > 0) {
			this.oDivGoTop.style.display = 'block';
			startMove(this.oDivGoTop, {opacity: 100})
		}
		else {
			startMove(this.oDivGoTop, {opacity: 0}, function () {
				_this.oDivGoTop.style.display = 'none';
			});
		}
		this.oDivGoTop.onclick = function () {
			_this.fnGoTop();
		}
	}
	else {
		this.oDivGoTop.style.display = 'none';
	}
}

/*GotoTop.prototype.fnGoTop = function (y) {
	
	var _this = this;
	clearInterval(this.timer);
	this.timer = setInterval(function () {
		y = y / 1.2;
		if (y <= 100) {
			y = 0;
			clearInterval(_this.timer);
		}
	}, 30);
}*/
GotoTop.prototype.fnGoTop = function () {
	var _this = this;
	clearInterval(this.timer);
	this.timer = setInterval(function () {
		if (document.body.scrollTop) {
			document.body.scrollTop /= 1.2;
			if (document.body.scrollTop <= 100) {
				document.body.scrollTop = 0;
				clearInterval(_this.timer);
			}
		}
		else if (document.documentElement.scrollTop) {
			document.documentElement.scrollTop /= 1.2;
			if (document.documentElement.scrollTop <= 100) {
				document.documentElement.scrollTop = 0;
				clearInterval(_this.timer);
			}
		}
	}, 30);
}

/*GotoTop.prototype.fnGoTop = function () {
	var y = document.documentElement.scrollTop || document.body.scrollTop;
	clearInterval(timer);
	var timer = setInterval(function () {
		y -= 100;
		if (y <= 100) {
			y = 0;
			clearInterval(timer);
		}
	}, 30);
}*/



